<?php
// 分享家入口
class Controller_App_Share extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/share/default";
        parent::__construct($req, $res);
    }

    // 晒单
    public function action_order()
    {
        $this->template->set_filename('smarty:app/share/order');
    }

    // 默认入口
    public function action_index ($shopId)
    {
        App_Shop::setCurrentShopId($shopId);
        $shop = new App_Share_Shop($shopId);
        $shared = $shop->isSharedByUser($this->_user);
        $comeId = $this->request->query('come');
        if(!empty($comeId)){
            $this->template->set_filename('smarty:app/share/come');
            return ;
        }
        if (! $shared)
        {
            $this->request->redirect('/share/join/'.$shopId);
        }
        else
        {
            $account = $shop->account($this->_user);
            $this->template->set_filename('smarty:app/share/my');
            $this->template->set('account', $account);
        }
    }

    // 用户参与分享的店铺列表
    public function action_shops ($shopId)
    {
        $shops = App_Share_User::shopList($this->_uid);
        $this->template->set_filename('smarty:app/share/shops');
        $this->template->set('shops', $shops);
    }

    // 英雄榜：店面所有分享家成员排名  (type = all)
    // 英雄榜：本店我的分享团队成员列表 (type = my)
    public function action_sort_sharers($shopId, $type = 'all')
    {
        $shop = new App_Share_Shop($shopId);
        $users = $shop->getShareUsers($type, $this->_uid, 'incomeMoney');
        $currUser = $shop->user_rank($users,$this->_uid);
        $this->template->set('type', $type);
        $this->template->set('currUser', $currUser);
        $this->template->set('users', $users);
        $this->template->set_filename('smarty:app/share/sharers');
    }

    // 本店我的团队成员列表
    public function action_team ($shopId)
    {
        $shop = new App_Share_Shop($shopId);
        $users = $shop->getShareUsers('my', $this->_uid, 'created');
        $this->template->set('users', $users);
        $this->template->set_filename('smarty:app/share/team');
    }

    // 收益
    public function action_income ()
    {
        $shopId  = App_Shop::currentShopId();
        $shop    = new App_Share_Shop($shopId);
        $account = $shop->account($this->_user);
        $incomeList = App_Share_User::incomeList($this->_uid, $shopId, 0, 20);

        $this->template->set('account', $account);
        $this->template->set('incomeList', $incomeList);
        $this->template->set_filename('smarty:app/share/income_money');
    }

    // 提现
    public function action_cash ()
    {
        $shopId  = App_Shop::currentShopId();

        $shop    = new App_Share_Shop($shopId);
        $user    = new App_Share_User($this->_user);

        if (! empty($this->_post))
        {
            $txMoney = (int) (Arr::get($this->_post, 'money') * 100);
            $txAccount = Arr::get($this->_post, 'alipayAccount');
            $txUserName = Arr::get($this->_post, 'alipayUserName');

            $user->cashMoney($txMoney, $txAccount, $txUserName);
            $this->request->redirect('/share/cash');
        }

        $account = $shop->account($this->_user);
        $records = $user->cashRecords();
        $moneyCashing = $user->getMoneyCashing();

        $this->template->set_filename('smarty:app/share/income_cash');
        $this->template->set('account', $account);
        $this->template->set('moneyCashing', $moneyCashing);
        $this->template->set('records', $records);
    }

    // 兑换金币
    public function action_coin ()
    {
        $shopId  = App_Shop::currentShopId();

        $shareUser   = new App_Share_User($this->_user);

        if (! empty($this->_post))
        {
            $moneyYuan = floatval(Arr::get($this->_post, 'money'));
            if ($moneyYuan > 0)
            {
                $money = (int) ($moneyYuan * 100);
                $shareUser->money2Coin($money);
                $this->request->redirect('/share/coin');
            }
        }

        // 分享家账户(资金)
        $accountMoney      = $shareUser->account();

        // 买家账户(金币)
        $accountBuyer      = App_Account::account($this->_user);
        $money2CoinRecords = App_Account::money2coinRecords($this->_uid);
        $coinUsedRecords   = App_Account::coinUsedRecords($this->_uid);

        $this->template->set_filename('smarty:app/share/income_coin');
        $this->template->set('accountMoney', $accountMoney);
        $this->template->set('accountBuyer', $accountBuyer);
        $this->template->set('money2CoinRecords', $money2CoinRecords);
        $this->template->set('coinUsedRecords', $coinUsedRecords);
    }

    // 申请
    public function action_join($shopId)
    {
        if (empty($this->_post))
        {
            $this->template->set_filename('smarty:app/share/join');
        }
        else
        {
            $userName = Arr::get($this->_post, 'shareUserName'); 
            $mobile   = Arr::get($this->_post, 'mobile');
            $uid      = $this->_user->getUserUid();

            $shop = new App_Share_Shop($shopId);
            $shop->createShareUser($uid, $userName, $mobile);

            $this->request->redirect('/share/index/' . $shopId.'?come=1');
        }
    }

    public function action_other(){
        $other = new App_Share_Shopincome();
        $income = $other->shop_income($this->_user);
        $total_income = 0;
        foreach($income as $v){
            $total_income += $v['incomeMoney'];
        }
        
        $this->template->set('income', $income);
        $this->template->set('total_income', $total_income);
        $this->template->set_filename('smarty:app/share/other');
    }

    public function action_qrcode ($shopId = NULL)
    {
        $shopId = $shopId > 0 ? $shopId : App_Shop::currentShopId();
        $uid    = $this->_uid;
        $link = rawurlencode(url::site("/homepage/shop/$shopId/$uid", 'http', FALSE));
        $logo = App_Shop::shopIcon($shopId);
        $logo = rawurlencode($logo);
        $qrCodeImgSrc = "http://qr.liantu.com/api.php?w=640&text=$link&logo=$logo";

        $this->template->set_filename('smarty:app/share/qrcode');
        $this->template->set('qrCodeImageSrc', $qrCodeImgSrc);
    }

    public function action_i ($shareUid, $shopId)
    {
        App_Share_User::initShareInvite($this->_uid, $shareUid, $shopId);
        $url = $this->request->query('url');
        $this->request->redirect($url);
    }

}
